// pages/Tax/Tax.js
var tax = require("./TaxAlgorithm.js").tax; 
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx);
    tax._init(this);
    tax.setDisplay("tax-type-buy");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  tax_typeChange:function(event){
    var selectVal = event.detail.value;
    var id = event.currentTarget.dataset.id;
    var value=event.detail.value;
    var types = event.currentTarget.dataset.types;

    tax.setDisplay(selectVal);
    tax.setParam(id, value, types);
    app.console.log(event,1);
    //data-txt data-rate
  },
  radioChange:function(event){
    var selectVal = event.detail.value;
    var id = event.currentTarget.dataset.id;
    var value = event.detail.value;
    var types = event.currentTarget.dataset.types;
    var txt = event.currentTarget.dataset.txt;
    var text = event.currentTarget.dataset.text;
    
  
    if (value == "HK" &&text !== undefined){
      var txtData = "agParam." + text;
     
      //var data = (this.data.agParam[text]*1.03).toFixed(2);
      var textVal = tax.taxTools.toThousands((new Number(this.data.agParam[text].replace(/,/g,"")) * 1.03).toFixed(2)).replace(/,\./g, ".");
      app.console.log("APP:" + this.data.agParam[text]);
      this.setData({
        [txtData]: textVal
      });
    }
    else{
      if (text !== undefined){
        var txtData = "agParam." + text;

        var textVal = tax.taxTools.toThousands(new Number(this.data.agParam[txt]));
    
        this.setData({
          [txtData]: textVal
        });        
        
      }
      
    }


    if (id ==="tax_first"){
      if(value==="true"){
        this.setData({
          "displayObj.tax_gtax_display": false,
          "displayObj.tax_extraTax_display":false,
          "displayObj.tax_isGtax": false,
        });       
      }else{
        this.setData({
          //"displayObj.tax_gtax_display": false,
          "displayObj.tax_extraTax_display": true,
          "displayObj.tax_isGtax": true,

        });       
      }
    }
    if (id ==="tax_isGtax"){
      if (value=="true"){
        this.setData({
          "displayObj.tax_gtax_display":true
        });
      }else{
      
        this.setData({
          "displayObj.tax_gtax_display": false
        });     
      }

    }

    console.log("id is",id);

    tax.setParam(id, value, types);
    app.console.log(this.data);
    
    


    
  },
  txtChange:function(event){
    var selectVal = event.detail.value;
    var id = event.currentTarget.dataset.id;
    var value = event.detail.value;
    var types = event.currentTarget.dataset.types;
    var rate = event.currentTarget.dataset.rate;
    var text = event.currentTarget.dataset.text;

    app.console.log(types);
    app.console.log(this.data, 1);
    

    if(rate!=undefined){
      rate = this.data.agParam[rate];
    }

    if (value.split('.').length>2) {
      value =  value.substring(0, value.length - 1);
    }
    if (rate != undefined && rate == "HK"&&text!==undefined) {
      app.console.log(text);
      var textData = "agParam." + text;
      var textVal = tax.taxTools.toThousands((new Number(value) * 1.03).toFixed(2)).replace(/,\./g,".");
      console.log((value * 1.03).toFixed(2));
      this.setData({
        [textData]: textVal
      });
    }
   
    else{
      if(text!=undefined){
        var textData = "agParam." + text;
        var textVal = tax.taxTools.toThousands(new Number(value));
        this.setData({
          [textData]: (textVal)
        });     
      }

    }

    tax.setParam(id, value, types);
    this.setData({
      "vf.rtx_text": false,
      "vf.loan_text": false,
      "vf.rate_text": false,
      "vf.floor_text": false,
    });


    
    return value;

  },
  btn_calculate:function(event){
    

    var canSubmit=true;
    var agParam=this.data.agParam;
    console.log(agParam);
    //tip_register txt_floor txt_loan txt_rate
    if (agParam.tax_type =="tax-type-buy"){
  
      if (agParam.txt_register == "" || agParam.txt_register==0){
        canSubmit=false;
        this.setData({
          "vf.rtx_text":true
        });
      } 
    }else if (agParam.tax_type =="tax-type-buyAndMortgage"){


      if (agParam.txt_register == "" || agParam.txt_register == 0) {
        canSubmit = false;
        this.setData({
          "vf.rtx_text": true
        });
      }
      if (agParam.txt_loan == "" || agParam.txt_loan == 0) {
        canSubmit = false;
        this.setData({
          "vf.loan_text": true
        });
      }    

      if (agParam.txt_rate == "" || agParam.txt_rate == 0 && txt_rate <= 0 && txt_rate > 100) {
        canSubmit = false;
        this.setData({
          "vf.rate_text": true
        });
      }  


    }else if (agParam.tax_type == "tax-type-mortgage"){


      if (agParam.txt_loan == "" || agParam.txt_loan == 0) {
        canSubmit = false;
        this.setData({
          "vf.loan_text": true
        });
      }    

     
      if (agParam.txt_rate == "" || new Number(agParam.txt_rate)*100 <= 0 || new Number(agParam.txt_rate)*100 >=100 ){
        console.log("超出");
        canSubmit = false;
        this.setData({
          "vf.rate_text": true
        });
      }  


    }else if (agParam.tax_type == "tax-type-register"){
      if (agParam.txt_floor == "" || agParam.txt_floor == 0) {
        canSubmit = false;
        this.setData({
          "vf.floor_text": true
        });
      }
    }
    
    

    //rtx_text loan_text floor_text rate_text
    if (canSubmit){
      var resultObj=tax.getResult();
      wx.setStorage({
        key: 'taxResult',
        data: resultObj
      });
      wx.navigateTo({
        url: '/pages/Tax/TaxResult/TaxResult',
      });
    }
    

    
    
    console.log(this.data);
  }



});